/**
 * @author: welling
 */
Weifox.apps.tencent = {
	name:'fanfou',
	domain:'http://api.fanfou.com/',
	icon:'',
	appKey:'c8fa581af8a049b7b9a7d2b9d1a85370',
	appSecret:'529a41ac76c693b7c725e97072ce177c',
	method:{
		oauth:1
	},
	
	handleError:function(t){
		t.__a="tencent error";
		//Weifox.debug.showObj(t);
	},
	/**
	 * 修正腾讯图显示不出来的Bug
	 * @param {Object} url
	 */
	imageFix:function(url){
		//var u=url.replace('app','t0');
		var u=u+'/460';
		return u;
	},
	parseText:function(node,text){
		node.appendChild(document.createTextNode(text));
	},
	/***
	 * 发微博
	 * @param {Object} user
	 * @param {Object} data
	 */
	send:function(user,data){
		var req={};
		var encode=Weifox.net.rfcEncode;
		var net=Weifox.net;
		data.text=net.rfcDecode(data.text);
		switch(data.type){
			case 'nl':
				req.url='http://open.t.qq.com/api/t/add';
				req.args=[['content', data.text],['clientip','120.73.5.6']];
				break;
			case 're-cm':
			case 'cm':
				req.url='http://api.fanfou.com/statuses/update.json';
				data.text=encode('@'+data.sender)+' '+data.text;
				req.args=[['status', data.text],['in_reply_to_status_id',data.id]];
				break;
			case 'rp':
				req.url='http://api.fanfou.com/statuses/update.json';
				data.text=data.text+' '+"%E8%BD%AC%3A%40"+encode(data.sender+' '+data.content);
				req.args=[['status', data.text],['repost_status_id',data.id]];
				break;
			case 're-dm':
				req.url='http://api.fanfou.com/direct_messages/new.json';
				req.args=[['text', data.text],['user',data.id]];
				req.args.push(['in_reply_to_id',data.in_reply_to_id ]);
				break;
			case 'nl-pic':
				req.url='http://open.t.qq.com/api/t/add_pic';
				req.args=[['content', net.JSStringToUTF8(data.text)],['clientip','120.73.5.6']];
				req.stream={
					pic:data.pic
				}
				break;
			default:
				return;
		}
//		Weifox.debug.showObj(data);
		this.sendRequest(user,req);
	},
	
	/**
	 * 
	 * @param {Object} method
	 * @param {Object} para
	 * @param {Object} callback
	 */
	verify:function(method,para,callback){
		
		if(method=='oauth'){////oauth
			var tencent=this;
			var data={
				url:'https://open.t.qq.com/cgi-bin/access_token',
				success:function(text){//获取到两个参数后,还要发一条获取用户信息...双重回调,TODO:优化
					var a=text.split('&');
					var token=a[0].split('=')[1];
					var token_secret=a[1].split('=')[1];
					var user={
						app:'tencent',
						method:'oauth',
						para1:token,
						para2:token_secret
					}
					//
					//获取用户的具体信息
					tencent.sendRequest(user,{
						url:'http://open.t.qq.com/api/user/info',
						success:function(u){
							u=u.data;
							//hack
							u.head=u.head.replace('app','t0');
							u.head+='/50';
							callback({
								app:'tencent',
								method:'oauth',
								para1:token,
								para2:token_secret,
								user_id:u.name,
								user_name:u.nick,
								user_icon:u.head
							});
						},
						error:callback
					});
				},
				error:callback
			}
			var net=Weifox.net;
			net.makeOAuth(data,this.appKey,this.appSecret,{
				oauth_verifier:para.pn,
				oauth_token:para.token,
				oauth_token_secret:para.token_secret,
				oauth_callback:''
			},false);
			new net.Request().send(data);
		}
	},
	getOAuthCode:function(callback){
		var data={
			method:'GET',
			url:'https://open.t.qq.com/cgi-bin/request_token',
			success:function(text){
				
				var x=text.split('&');
				var token=x[0].split('=')[1];
				var token_secret=x[1].split('=')[1];;
				Weifox.browser.openLink('https://open.t.qq.com/cgi-bin/authorize?oauth_token='
					+token+'&callback=null',true);
				callback({
					token:token,
					token_secret:token_secret
				});
			},
			error:callback
		};
		var net=Weifox.net;
		net.makeOAuth(data,this.appKey,this.appSecret,{oauth_consumer_secret:''},false);
		new net.Request().send(data);
	},
	/**
	 * 
	 * 返回一个封好数据的Request,直接Send即可,
	 * Data需先设置好再传入,否则OAuth的签名计算会出错.
	 * 适用于已授权的API,
	 * @param {Object} user
	 * @param {Object} data
	 */
	sendRequest:function(user,data){
		var method=user.method.toUpperCase();
		if(!data.args)data.args=[];
		data.args.push(['format','json']);
		if(method=='OAUTH'){
			Weifox.net.makeOAuth(data,this.appKey,this.appSecret,
					{oauth_token:user.para1,oauth_token_secret:user.para2,oauth_callback:'null'},false);
		}else{
			throw new Error('This app does not support '+method);
		}
		var req=new Weifox.net.Request();
		data.error=this.handleError;
		req.send(data);
	}
}
/*
{"data":{
	"hasnext":1,
	"info":[
		{"city_code":"53",
		"count":0,
		"country_code":"1",
		"from":"网页"
		,"fromurl":"网页",
		"geo":0,
		"head":"http://app.qlogo.cn/mbloghead/0985eefe60ba788c37ee",
		"id":"9566074355659",
		"image":null,
		"isvip":0,"location":"广东 云浮","mcount":0,
		"name":"loveweifox",
		"nick":"Weifox",
		"origtext":"@api_weibo 你好,我请问一个OA过?",
		"province_code":"44","self":1,"source":null,
		"status":0,
		"text":"@api_weibo 你好,我请问一个OAuth授权的题,为什么第一步又可以通得过?",
		"timestamp":1301821385,"type":5
		}*/
